﻿using SCG.General;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TTEngine
{
    public static class MapObjectFactory
    {
        //static constructor
        static MapObjectFactory()
        {
            TypeLoader loader = new TypeLoader("MapObject", "");
            foreach (Type t in loader.Types)
            {
                MapObject mo = (MapObject)loader.CreateInstance(t);
                _templates.Add(mo);
                if (mo.LootLevel >= 0)
                    _lootTemplates.Add(mo);
                if (mo.DressingLevel > 0)
                    _dressingTemplates.Add(mo);
            }
        }

        //get a new instance
        public static MapObject GetMapObject(string objectName)
        {
            foreach (MapObject mo in _templates)
                if (mo.ObjectName.ToUpper() == objectName.ToUpper())
                    return mo.Clone();
            return null;
        }
        public static MapObject GetMapObject(string objectName, int qty)
        {
            MapObject mo = GetMapObject(objectName);
            if (mo == null)
                return null;
            mo.Quantity = qty;
            return mo;
        }

        //get some loot
        public static MapObject GetLoot(int difficultyLevel)
        {
            RandomList<MapObject> candidates = new RandomList<MapObject>();
            foreach (MapObject mo in _lootTemplates)
                if (mo.LootLevel <= difficultyLevel)
                    candidates.Add(mo);
            if (candidates.Count == 0)
                return null;
            MapObject loot = candidates.RandomValue.Clone();
            loot.LootProcessing(difficultyLevel);
            return loot;
        }

        //get dungeon dressing
        public static MapObject GetDressing(int difficultyLevel)
        {
            RandomList<MapObject> candidates = new RandomList<MapObject>();
            foreach (MapObject mo in _dressingTemplates)
                if (mo.DressingLevel <= difficultyLevel)
                    candidates.Add(mo);
            if (candidates.Count == 0)
                return null;
            return candidates.RandomValue.Clone();
        }

        //private members
        private static List<MapObject> _templates = new List<MapObject>();
        private static List<MapObject> _lootTemplates = new List<MapObject>();
        private static List<MapObject> _dressingTemplates = new List<MapObject>();
    }
}
